﻿Imports MindFusion.Diagramming
Imports MindFusion.Drawing
'represents custom behavior that defines how FlowChart.NET reacts to users' actions with a mouse or other pointing devices

Public Class ClsCustomBehavior
    Inherits WinForms.Behaviors.BehaviorBase

    Public Event POKUS()

#Region "Attributes"
#Region "Private attributes"
    'what to do if user is dragging mouse - create table or drag selection rectangle
    Private MeCursorOperationOnDragging As GePrimaryOperationOfHand = GePrimaryOperationOfHand.kkNeutral
#End Region
#End Region
#Region "Constructors"
    Sub New(ByVal diagramView As MindFusion.Diagramming.WinForms.DiagramView)
        MyBase.New(diagramView)
    End Sub
#End Region
#Region "Properties"
#Region "Public properties"
    Public Property CursorOperationOnDragging() As GePrimaryOperationOfHand
        Get
            Return MeCursorOperationOnDragging
        End Get
        Set(ByVal IeValue As GePrimaryOperationOfHand)
            MeCursorOperationOnDragging = IeValue
        End Set
    End Property
#End Region
#End Region
#Region "Functions"
#Region "Overrides & Overloads Functions"
    'overrided sub for setting mouse cursor
    Public Overrides Function SetMouseCursor(ByVal point As System.Drawing.PointF, ByRef startInteraction As Boolean) As MindFusion.Diagramming.CursorHint
        'Debug.Print("SetMouseCursor")
        Dim LoItem As DiagramItem = Nothing
        'determine operation from cursors coordinates
        Select Case MMGetCursorOperationFromCursorsCoordinates(point, LoItem)
            Case GeCursorOperation.kkSelectionRectangle
                Return CursorHint.Pointer
            Case GeCursorOperation.kkTableMove, GeCursorOperation.kkLinkMove, GeCursorOperation.kkContainerMove, GeCursorOperation.kkShapeNodeMove
                Return CursorHint.Move
            Case GeCursorOperation.kkLinkCreate
                Return CursorHint.AllowLink
            Case GeCursorOperation.kkTableCreate, GeCursorOperation.kkContainerCreate
                Return CursorHint.Pointer
            Case GeCursorOperation.kkTopLeftCornerModification
                Return CursorHint.DiagonalResize
            Case GeCursorOperation.kkTopCenterModification
                Return CursorHint.VerticalResize
            Case GeCursorOperation.kkTopRightCornerModification
                Return CursorHint.CounterDiagonalResize
            Case GeCursorOperation.kkMiddleRightModification
                Return CursorHint.HorizontalResize
            Case GeCursorOperation.kkBottomRightCornerModification
                Return CursorHint.DiagonalResize
            Case GeCursorOperation.kkBottomCenterModification
                Return CursorHint.VerticalResize
            Case GeCursorOperation.kkBottomLeftCornerModification
                Return CursorHint.CounterDiagonalResize
            Case GeCursorOperation.kkMiddleLeftModification
                Return CursorHint.HorizontalResize
            Case Else
                Debug.Print("CHYBA!!! - SetMouseCursor")
                Return Nothing
        End Select
    End Function
    'overrided sub for starting drawing
    Public Overrides Function StartDraw(ByVal point As PointF) As InteractionState
        Debug.Print("StartDraw")
        Dim LoItem As DiagramItem = Nothing
        Dim LiManipulator As Integer = 0 'using to identify controlpoint of a link
        'bug? for sure :-)
        DiagramView.CurrentCursor = DiagramView.CurrentCursor
        'determine operation from cursors coordinates
        Select Case MMGetCursorOperationFromCursorsCoordinates(point, LoItem, LiManipulator)
            Case GeCursorOperation.kkSelectionRectangle
                Return New InteractionState(Diagram.Selection, -1, Action.Create)
            Case GeCursorOperation.kkLinkCreate
                Dim LoMMLinkNode As ClsMMLinkNode = New ClsMMLinkNode(Diagram)
                LoMMLinkNode.Origin = LoItem
                Return New InteractionState(LoMMLinkNode, -1, Action.Create)
            Case GeCursorOperation.kkTableCreate
                Return New InteractionState(New ClsMMTableNode(Diagram), -1, Action.Create)
            Case GeCursorOperation.kkContainerCreate
                Return New InteractionState(New ClsMMContainerNode(Diagram), -1, Action.Create)
            Case GeCursorOperation.kkLinkMove
                Return New InteractionState(LoItem, LiManipulator, Action.Modify)
            Case GeCursorOperation.kkTableMove, GeCursorOperation.kkContainerMove, GeCursorOperation.kkShapeNodeMove
                Return New InteractionState(LoItem, 8, Action.Modify)
            Case GeCursorOperation.kkTopLeftCornerModification
                Return New InteractionState(LoItem, 0, Action.Modify)
            Case GeCursorOperation.kkTopCenterModification
                Return New InteractionState(LoItem, 4, Action.Modify)
            Case GeCursorOperation.kkTopRightCornerModification
                Return New InteractionState(LoItem, 1, Action.Modify)
            Case GeCursorOperation.kkMiddleRightModification
                Return New InteractionState(LoItem, 5, Action.Modify)
            Case GeCursorOperation.kkBottomRightCornerModification
                Return New InteractionState(LoItem, 2, Action.Modify)
            Case GeCursorOperation.kkBottomCenterModification
                Return New InteractionState(LoItem, 6, Action.Modify)
            Case GeCursorOperation.kkBottomLeftCornerModification
                Return New InteractionState(LoItem, 3, Action.Modify)
            Case GeCursorOperation.kkMiddleLeftModification
                Return New InteractionState(LoItem, 7, Action.Modify)
            Case Else
                Debug.Print("CHYBA!!! - StartDraw")
                Return Nothing
        End Select
    End Function
#End Region
#Region "MM Functions"
    'sub for determination operation from cursors coordinates
    Private Function MMGetCursorOperationFromCursorsCoordinates(ByVal IoPointF As PointF, ByRef OoItem As DiagramItem, Optional ByRef OiManipulator As Integer = 0) As GeCursorOperation
        'Debug.Print("GetCursorOperationFromCursorsCoordinates")
        Dim LoItem As DiagramItem = Diagram.GetItemAt(IoPointF, False)

        If LoItem IsNot Nothing Then
            OoItem = LoItem
            Select Case LoItem.GetType.Name
                Case "ClsMMTableNode"
                    Dim LoMMTableNode As ClsMMTableNode = CType(LoItem, ClsMMTableNode)
                    Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMTableNode.Bounds, 2)
                        Case GeEdgeForResize.kkTopLeftCorner
                            Return GeCursorOperation.kkTopLeftCornerModification
                        Case GeEdgeForResize.kkTop
                            Return GeCursorOperation.kkTopCenterModification
                        Case GeEdgeForResize.kkTopRightCorner
                            Return GeCursorOperation.kkTopRightCornerModification
                        Case GeEdgeForResize.kkRight
                            Return GeCursorOperation.kkMiddleRightModification
                        Case GeEdgeForResize.kkBottomRightCorner
                            Return GeCursorOperation.kkBottomRightCornerModification
                        Case GeEdgeForResize.kkBottom
                            Return GeCursorOperation.kkBottomCenterModification
                        Case GeEdgeForResize.kkBottomLeftCorner
                            Return GeCursorOperation.kkBottomLeftCornerModification
                        Case GeEdgeForResize.kkLeft
                            Return GeCursorOperation.kkMiddleLeftModification
                        Case GeEdgeForResize.kkNoEdge
                            If LoMMTableNode.MMRectangleForNewLink.Contains(IoPointF) Then
                                Return GeCursorOperation.kkLinkCreate
                            Else
                                Return GeCursorOperation.kkTableMove
                            End If
                    End Select
                Case "ClsMMLinkNode"
                    If LoItem.HitTestHandle(IoPointF, OiManipulator) Then
                        'dragging a controlpoint of link
                        Return GeCursorOperation.kkLinkMove
                    Else
                        'nothing
                        Return GeCursorOperation.kkSelectionRectangle
                    End If
                Case "ClsMMContainerNode"
                    Dim LoMMContainerNode As ClsMMContainerNode = CType(LoItem, ClsMMContainerNode)
                    If LoMMContainerNode.Folded Then
                        Return GeCursorOperation.kkContainerMove
                    Else
                        Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMContainerNode.Bounds, 2)
                            Case GeEdgeForResize.kkTopLeftCorner
                                Return GeCursorOperation.kkTopLeftCornerModification
                            Case GeEdgeForResize.kkTop
                                Return GeCursorOperation.kkTopCenterModification
                            Case GeEdgeForResize.kkTopRightCorner
                                Return GeCursorOperation.kkTopRightCornerModification
                            Case GeEdgeForResize.kkRight
                                Return GeCursorOperation.kkMiddleRightModification
                            Case GeEdgeForResize.kkBottomRightCorner
                                Return GeCursorOperation.kkBottomRightCornerModification
                            Case GeEdgeForResize.kkBottom
                                Return GeCursorOperation.kkBottomCenterModification
                            Case GeEdgeForResize.kkBottomLeftCorner
                                Return GeCursorOperation.kkBottomLeftCornerModification
                            Case GeEdgeForResize.kkLeft
                                Return GeCursorOperation.kkMiddleLeftModification
                            Case GeEdgeForResize.kkNoEdge
                                Return GeCursorOperation.kkContainerMove
                        End Select
                    End If
                Case "ClsMMShapeNode"
                    Dim LoMMShapeNode As ClsMMShapeNode = CType(LoItem, ClsMMShapeNode)
                    Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMShapeNode.Bounds, 2)
                        Case GeEdgeForResize.kkTopLeftCorner
                            Return GeCursorOperation.kkTopLeftCornerModification
                        Case GeEdgeForResize.kkTop
                            Return GeCursorOperation.kkTopCenterModification
                        Case GeEdgeForResize.kkTopRightCorner
                            Return GeCursorOperation.kkTopRightCornerModification
                        Case GeEdgeForResize.kkRight
                            Return GeCursorOperation.kkMiddleRightModification
                        Case GeEdgeForResize.kkBottomRightCorner
                            Return GeCursorOperation.kkBottomRightCornerModification
                        Case GeEdgeForResize.kkBottom
                            Return GeCursorOperation.kkBottomCenterModification
                        Case GeEdgeForResize.kkBottomLeftCorner
                            Return GeCursorOperation.kkBottomLeftCornerModification
                        Case GeEdgeForResize.kkLeft
                            Return GeCursorOperation.kkMiddleLeftModification
                        Case GeEdgeForResize.kkNoEdge
                            If LoMMShapeNode.MMRectangleForNewLink.Contains(IoPointF) Then
                                Return GeCursorOperation.kkLinkCreate
                            Else
                                Return GeCursorOperation.kkShapeNodeMove
                            End If
                    End Select
                Case Else
                    Debug.Print("CHYBA!!! - GetCursorOperationFromCursorsCoordinates")
            End Select
        Else
            Return GeCursorOperation.kkSelectionRectangle
            'Select Case MeCursorOperationOnDragging
            '    Case GePrimaryOperationOfHand.kkNeutral
            '        Return GeCursorOperation.kkSelectionRectangle
            '    Case GePrimaryOperationOfHand.kkTable
            '        Return GeCursorOperation.kkTableCreate
            '    Case GePrimaryOperationOfHand.kkContainer
            '        Return GeCursorOperation.kkContainerCreate
            '    Case Else
            '        Debug.Print("CHYBA!!! - GetCursorOperationFromCursorsCoordinates - Select Case MeCursorOperationOnDragging")
            'End Select
        End If
    End Function
#End Region
#End Region

End Class
